package com.alibaba.system.controller;

import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.PageList;
import com.alibaba.system.domain.Dictionarydetail;
import com.alibaba.system.query.DictionarydetailQuery;
import com.alibaba.system.service.IDictionarydetailService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionarydetail")
public class DictionarydetailController {

    @Autowired
    private IDictionarydetailService dictionarydetailService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //http://localhost:8080/dictionarydetail/1 + get
    @GetMapping("/{id}")
    public Dictionarydetail findOne(@PathVariable("id")Long id){
        return dictionarydetailService.findById(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    //http://localhost:8080/dictionarydetail + 无 + get
    @GetMapping
    public List<Dictionarydetail> findAll(){
        return dictionarydetailService.findAll();
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //http://localhost:8080/dictionarydetail + {json对象} + post

    @PostMapping
    public PageList<Dictionarydetail> queryPage(@RequestBody DictionarydetailQuery query){
        return dictionarydetailService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    //http://localhost:8080/dictionarydetail/1 + delete
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionarydetailService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param dictionarydetail
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/dictionarydetail + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){
        try {
            if(dictionarydetail.getId() == null){//添加
                dictionarydetailService.add(dictionarydetail);
            }else{
                dictionarydetailService.update(dictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



    /**
     * 接口：批量删除
     * @param
     * @return
     */
    @PatchMapping
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            dictionarydetailService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    @GetMapping("findByTypesId")
    public PageInfo<Dictionarydetail> findByTypesId(@RequestParam Long typesId, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return dictionarydetailService.findByTypesId(typesId, pageNum, pageSize);
    }
}
